package hu.crs;

import hu.crs.coordinate.Coordinate;
import hu.crs.game.HeroProperties;
import hu.crs.game.entity.creature.Creature;
import hu.crs.game.entity.creature.hero.Hero;
import hu.crs.game.entity.creature.monster.Monster;
import hu.crs.game.entity.creature.monster.ant.GiantAnt;
import hu.crs.game.field.Floor;
import hu.crs.game.field.Impassable;
import hu.crs.game.field.Wall;
import hu.crs.game.handler.GameFlowHandler;
import hu.crs.game.registry.MonsterRegister;
import hu.crs.game.statespace.GameMap;
import hu.crs.game.statespace.MapElement;

import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.Position;

public class RogueApp extends JFrame implements KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7181651924052539575L;
	/**
	 * @param args
	 */
	private RogueApp r;
	private JTextArea mapArea;
	private HeroProperties hp = new HeroProperties();
	private GameMap gm = new GameMap();
	private MonsterRegister mr = new MonsterRegister();
	private GameFlowHandler gfh = new GameFlowHandler(gm, mr, hp);

	private void initMap(HeroProperties hp) {
		for (int j = 0; j < 24; j++) {
			for (int i = 0; i < 80; i++) {
				if (i == 0 || j == 0 || i == 79 || j == 23) {
					gm.set(i, j, new MapElement(new Wall()));
				} else {
					gm.set(i, j, new MapElement(new Floor()));
				}
			}
		}

		gm.add(hp.getPosition(), hp.getHero());
		gfh.register(10, 10, new GiantAnt());
	}



	private void handleKeypress(KeyEvent arg0) {
		Coordinate c = new Coordinate(hp.getPosition().getX(), hp.getPosition()
				.getY());
		if (arg0.getKeyChar() == 'h') {
			c.setX(hp.getPosition().getX() - 1);
			c.setY(hp.getPosition().getY());
		} else if (arg0.getKeyChar() == 'j') {
			c.setX(hp.getPosition().getX());
			c.setY(hp.getPosition().getY() + 1);
		} else if (arg0.getKeyChar() == 'k') {
			c.setX(hp.getPosition().getX());
			c.setY(hp.getPosition().getY() - 1);
		} else if (arg0.getKeyChar() == 'l') {
			c.setX(hp.getPosition().getX() + 1);
			c.setY(hp.getPosition().getY());
		}

		gfh.finishTurn(c);	
		mapArea.setText(gm.getMapString());
	}



	public static void main(String[] args) {
		RogueApp r = new RogueApp();
		//r.hp = new HeroProperties();
		r.initMap(r.hp);
		// r.setPreferredSize(new DimensionUIResource(1024, 768));
		r.mapArea = new JTextArea();
		r.mapArea.addKeyListener(r);
		Font font = new Font("Monospaced", Font.PLAIN, 18);
		r.mapArea.setFont(font);
		r.mapArea.setText(r.gm.getMapString());
		r.add(r.mapArea);
		r.pack();
		r.setVisible(true);
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		handleKeypress(arg0);

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

}
